
#include "ui_sensor_opt300x.h"
#include "main.h"
#include "bsp_opt300x.h"
#include "bsp_oled_spi.h"
#include <stdio.h>


float fLuxVal;

void app_SensorOpt300xInit(void)
{
    bsp_Opt300xInit();

    bsp_OledClearNoRefresh();
    bsp_OledShowChinese(0, 0, 0, 16, 1); //南京至一电子科技
    bsp_OledShowChinese(16, 0, 1, 16, 1); //南京至一电子科技
    bsp_OledShowChinese(32, 0, 2, 16, 1); //南京至一电子科技
    bsp_OledShowChinese(48, 0, 3, 16, 1); //南京至一电子科技
    bsp_OledShowChinese(64, 0, 4, 16, 1); //南京至一电子科技
    bsp_OledShowChinese(80, 0, 5, 16, 1); //南京至一电子科技
    bsp_OledShowChinese(96, 0, 6, 16, 1); //南京至一电子科技
    bsp_OledShowChinese(112, 0, 7, 16, 1); //南京至一电子科技
    bsp_OledShowString(4, 18, "OPT300X_SENSOR", 12, 1);
    bsp_OledShowString(2, 32, "Lux:", 16, 1);
    bsp_OledRefresh();

}

void app_SensorOpt300xDisplay(void)
{
    uint8_t ucKeyCode = KEY_NONE;
    char strBuff[14] = {0};

    app_SensorOpt300xInit();

    while(gSensorType == SENSOR_OPT300X)
    {
        /* 实现喂狗,Modbus轮询,刷新UI等操作，不可阻塞 */
        bsp_Idle();

        /* APP运行代码 */
        ucKeyCode = bsp_GetKey();
        if(ucKeyCode != KEY_NONE)
        {
            switch(ucKeyCode)
            {
            case KEY_1_UP:
            {
                bsp_BeepKeyTone();
                gSensorType = SENSOR_LMT70;
            };
            break;
            case KEY_1_LONG:
            {
                bsp_BeepKeyTone();

            };
            break;
            case KEY_2_UP:
            {
                bsp_BeepKeyTone();
                gSensorType = SENSOR_SHT2X;
            };
            break;
            case KEY_3_UP:
            {
                //bsp_BeepKeyTone();
                bsp_BeepStart(5, 1, 5); //蜂鸣器响5次，用于按键测试
            };
            break;
            case KEY_3_LONG:
            {

            };
            break;
            default:
                break;
            }
        }
        /* USER CODE END WHILE */
        if(bsp_CheckTimer(SOFT_TMR_ID_ONESECOND))
        {
            bsp_LedToggle(1);
            if(bsp_ReadOpt300xLux(OPT3001_ADDRESS_GND, &fLuxVal) != 0)
            {
                bsp_Opt300xInit();
            }

            SEGGER_RTT_SetTerminal(0);
            SEGGER_RTT_printf(0, "OPT300X SENSOR Data:\n\r");
            memset(strBuff, 0, sizeof(strBuff));
            sprintf(strBuff, "%0.1f", fLuxVal);


            /* 通过SEGGER-RTT 打印温度 */
            SEGGER_RTT_printf(0, "OPT300X LUX:%s\n\r", strBuff);
            /* 通过串口打印温度 */
            printf("OPT300X LUX:%s\n\r", strBuff);
            /* OLED 显示光照度 */
            bsp_OledShowString(42, 32, "            ", 16, 1);
            bsp_OledShowString(42, 32, strBuff, 16, 1);

            bsp_OledRefresh();
        }

    }
}
